package imagemorphing;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AnimationForm.java
 *
 * Created on 15-ago-2011, 19:20:47
 */

import javax.swing.JSlider;
import javax.swing.JFrame;
import java.awt.image.*;

/**
 *
 * @author Samuel
 */
public class ComplexAnimationForm extends JFrame {
    
    private BufferedImage sBuffer, dBuffer;
    private Triangulation sTriangulation, dTriangulation, aTriangulation;
    
    /** Creates new form AnimationForm */
    public ComplexAnimationForm(
            BufferedImage b, 
            Triangulation st,
            BufferedImage db,
            Triangulation dt) {
        sBuffer = b;
        dBuffer = db;
        sTriangulation = st;
        dTriangulation = dt;
        aTriangulation = new Triangulation();
        initComponents();
        frameSlider.setValue(0);
        morph(0.0);
        pack();
        setVisible(true);
    }
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        drawingPanel = new imagemorphing.SimpleDrawingPanel();
        frameSlider = new javax.swing.JSlider();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setLocationByPlatform(true);
        setResizable(false);

        drawingPanel.setPreferredSize(new java.awt.Dimension(430, 400));

        javax.swing.GroupLayout drawingPanelLayout = new javax.swing.GroupLayout(drawingPanel);
        drawingPanel.setLayout(drawingPanelLayout);
        drawingPanelLayout.setHorizontalGroup(
            drawingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 430, Short.MAX_VALUE)
        );
        drawingPanelLayout.setVerticalGroup(
            drawingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );

        frameSlider.setMaximum(30);
        frameSlider.setValue(0);
        frameSlider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                frameSliderStateChanged(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(drawingPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGroup(layout.createSequentialGroup()
                .addGap(106, 106, 106)
                .addComponent(frameSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(drawingPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(frameSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void morph(double c){
    aTriangulation = new Triangulation();
    aTriangulation.rebuildFrom(sTriangulation);
    aTriangulation.shapeTo(dTriangulation, c);
    drawingPanel.clr();
    TextureMapper.mixMapTexture(
            sBuffer, 
            sTriangulation, 
            aTriangulation,
            dBuffer,
            dTriangulation, 
            drawingPanel.getDBufferGraphics(),
            c);
    drawingPanel.repaint();
}
    
private void frameSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_frameSliderStateChanged
    JSlider source = (JSlider)evt.getSource();
    double c = (source.getValue() + 0.0) / source.getMaximum();
    morph(c);
}//GEN-LAST:event_frameSliderStateChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private imagemorphing.SimpleDrawingPanel drawingPanel;
    private javax.swing.JSlider frameSlider;
    // End of variables declaration//GEN-END:variables
}
